package org.darkforest.common.services.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.darkforest.common.services.ActivityService;
import org.darkforest.dao.mapper.ActivityGiftMapper;
import org.darkforest.dao.mapper.ActivityMapper;
import org.darkforest.model.Activity;
import org.darkforest.model.ActivityGift;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ActivityServiceImpl implements ActivityService {

    @Autowired
    private ActivityMapper activityMapper;

    @Autowired
    private ActivityGiftMapper activityGiftMapper;

    @Override
    @Transactional
    public int insertActivity(Activity activity) {
        // 活动的添加
        int insetActivityCount = activityMapper.insertSelective(activity);

        // 活动奖品的添加
        int giftCount = 0;
        int giftInsertCount = 0;

        // 有活动礼品时
        if (activity.getGiftList() != null && activity.getGiftList().size() > 0) {
            giftCount = activity.getGiftList().size();
            for(ActivityGift activityGift:activity.getGiftList()){
                activityGift.setActivityId(activity.getActivityId());
                int j = activityGiftMapper.insertSelective(activityGift);
                giftInsertCount = giftInsertCount + j;
            }
        }

        // 添加活动不成功时
        if (insetActivityCount != 1) {
            return 0;
        }

        // 添加活动的奖品不成功时
        if (giftInsertCount != giftCount) {
            return 0;
        }

        // 添加cg
        return 1;
    }

    @Override
    @Transactional
    public int updateActivity(Activity activity, String editGiftInfo) {

        // 活动的修改
        int activityUpdateCount = activityMapper.updateByPrimaryKeySelective(activity);

        // 修改的奖品
        int giftUpdateCount = 0;
        String[] giftUpdateInfo = null;
        if(StringUtils.isNotEmpty(editGiftInfo)){
            giftUpdateInfo = editGiftInfo.split(",");

            for (String giftRec: giftUpdateInfo) {
                // giftId:奖品件数
                String[] giftInfo = giftRec.split(":");
                ActivityGift record = new ActivityGift();
                record.setGiftId(Integer.valueOf(giftInfo[0]));
                record.setGiftStorage(Integer.valueOf(giftInfo[1]));
                int j = activityGiftMapper.updateByPrimaryKeySelective(record);
                giftUpdateCount = giftUpdateCount + j;
            }
        }

        // 添加的奖品
        int giftInsertCount = 0;
        int giftCount = 0;
        if (activity.getGiftList() != null && activity.getGiftList().size() > 0) {
            giftCount = activity.getGiftList().size();
            for(ActivityGift activityGift:activity.getGiftList()){
                activityGift.setActivityId(activity.getActivityId());
                int j = activityGiftMapper.insertSelective(activityGift);
                giftInsertCount = giftInsertCount + j;
            }
        }

        // 活动的修改件数
        if (activityUpdateCount != 1) {
            return 0;
        }

        // 修改的奖品的修改件数
        if (giftUpdateInfo != null && giftUpdateInfo.length > 0) {
            if (giftUpdateCount != giftUpdateInfo.length) {
                return 0;
            }
        }

        // 添加的奖品的添加件数
        if (activity.getGiftList() != null && activity.getGiftList().size() > 0) {
            if (giftInsertCount != giftCount) {
                return 0;
            }
        }

        return 1;
    }

    @Override
    @Transactional
    public int deleteActivity(String[] activityId) {

        int delCount = 0;

        for (String id : activityId) {
            // 活动奖品的删除
            ActivityGift activityGift = new ActivityGift();
            activityGift.setActivityId(Integer.parseInt(id));
            List<ActivityGift> activityGiftList = activityGiftMapper.selectListByCondition(activityGift);
            for (ActivityGift activityGiftDb : activityGiftList) {
                activityGiftMapper.deleteByPrimaryKey(activityGiftDb.getGiftId());
            }

            // 活动的删除
            int j = activityMapper.deleteByPrimaryKey(Integer.parseInt(id));
            delCount = delCount + j;
        }

        if (delCount != activityId.length) {
            return 0;
        }

        return 1;
    }
}
